package com.koalton.steward.common;

import org.junit.Test;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.LockSupport;

/**
 * Created on 2017/5/7  <br>
 * <p>
 * 1、单进程的全局序列
 * 2、获取当前进程ID
 * 3、获取当前机器的MAC地址
 *
 * @author koalton
 * @since 1.8
 */
public class Tool {

    public static final SimpleDateFormat Y2S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 单进程序列初始值
     */
    private static AtomicLong singleProcessSeq = new AtomicLong(new Date().getTime());

    /**
     * 支持单进程下获取递增序列
     *
     * @return 下一个可用的序列
     */
    public static long nextSeq() {
        return singleProcessSeq.getAndIncrement();
    }

    /**
     * 获取当前进程的PID
     *
     * @return 当前进程的PID
     */
    public static String getPID() {
        String name = ManagementFactory.getRuntimeMXBean().getName();
        return name.split("@")[0];

    }

    /**
     * 获取当前机器联网的MAC地址<br>
     * 如在笔记本中，一般有两块网卡，有线和无线，此方法用于获取正在用于网络工作的网卡
     *
     * @return
     */
    public static String getLocalHostMac() {
        String localHostMac = null;
        try {
            InetAddress inetAddressLocal = InetAddress.getLocalHost();
            localHostMac = getNetworkInterfaceMac(NetworkInterface.getByInetAddress(inetAddressLocal));
        } catch (UnknownHostException e) {
            //TODO koalton ，2017-5-7，预计2017-5-17 修复此问题
        } catch (SocketException e) {
            //TODO koalton ，2017-5-7，预计2017-5-17 修复此问题
        }

        return localHostMac;
    }

    /**
     * 获取当前机器所有的网络接口的MAC地址，不仅仅是IPV4接口
     *
     * @return
     */
    public static String getAllMacs() {
        StringBuilder allMacsBuf = new StringBuilder();
        Enumeration<NetworkInterface> allNetworkInterface;
        try {
            allNetworkInterface = NetworkInterface.getNetworkInterfaces();
            while (allNetworkInterface.hasMoreElements()) {
                NetworkInterface networkInterface = allNetworkInterface.nextElement();
                if (networkInterface == null)
                    continue;
                allMacsBuf.append(getNetworkInterfaceMac(networkInterface));
                allMacsBuf.append("\n");
            }
        } catch (SocketException e1) {
            //TODO koalton ,2017-5-7 ，预计2017-5-17 修复
        }
        return allMacsBuf.toString();
    }

    @Test
    public void testGetLocalHostMac() {
        String localHostMAC = getLocalHostMac();
        System.out.println(localHostMAC);
    }

    private static String getNetworkInterfaceMac(NetworkInterface networkInterface) {
        StringBuilder localHostMacBuf = new StringBuilder();
        try {
            byte[] mac = networkInterface.getHardwareAddress();
            if (null == mac) {
                return "";
            }
            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    localHostMacBuf.append("-");
                }
                //将mac地址中的每个值转换为可识别的16进制数字
                String macEl = "000000" + Integer.toHexString(mac[i]);
                macEl = macEl.substring(macEl.length() - 2);
                localHostMacBuf.append(macEl);
            }
        } catch (SocketException e) {
            //TODO koalton ，2017-5-7，预计2017-5-17 修复此问题
        }
        return localHostMacBuf.toString().toUpperCase();
    }

    @Test
    public void testGetAllMac() {
        System.out.println(getAllMacs());
    }


}
